﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerChiTietThanhPhanThucPhamDao : IChiTietThanhPhanThucPhamDao
    {
        public SqlServerChiTietThanhPhanThucPhamDao()
        {
            SQLUtility.TableName = "ChiTietThanhPhanThucPham";
        }

        //Trả về đối tượng báo ăn
        private ChiTietThanhPhanThucPham MakeChiTietThanhPhanThucPham(DataRow row)
        {
            Guid _id = Guid.Parse(row["Id"].ToString());
            Guid _thucPhamId = Guid.Parse(row["ThucPhamId"].ToString());
            Guid _thanhPhanThucPhamId = Guid.Parse(row["ThanhPhanThucPhamId"].ToString());
            double _luongTrenMotDonVi = double.Parse(row["LuongTrenMotDonVi"].ToString());
            bool _isThanhPhanChinh = bool.Parse(row["IsThanhPhanChinh"].ToString());
         return new ChiTietThanhPhanThucPham(_id, _thucPhamId, _thanhPhanThucPhamId, _luongTrenMotDonVi, _isThanhPhanChinh);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<ChiTietThanhPhanThucPham> MakeChiTietThanhPhanThucPhams(DataTable dt)  
        {
            IList<ChiTietThanhPhanThucPham> list = new List<ChiTietThanhPhanThucPham>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeChiTietThanhPhanThucPham(row));
            }
            return list;
        }

        public IList<Business_Objects.ChiTietThanhPhanThucPham> GetChiTietThanhPhanThucPham()
        {
            SQLUtility.TableName = "ChiTietThanhPhanThucPham";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeChiTietThanhPhanThucPhams(dt); 
        }

        public Business_Objects.ChiTietThanhPhanThucPham GetChiTietThanhPhanThucPhamByID(string ChiTietThanhPhanThucPhamID)
        {
            SQLUtility.TableName = "ChiTietThanhPhanThucPham";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", ChiTietThanhPhanThucPhamID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeChiTietThanhPhanThucPham(row);
            }
        }

        public void InsertChiTietThanhPhanThucPham(Business_Objects.ChiTietThanhPhanThucPham ChiTietThanhPhanThucPham)
        {
            SQLUtility.TableName = "ChiTietThanhPhanThucPham";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(ChiTietThanhPhanThucPham), true);
        }

        public int UpdateChiTietThanhPhanThucPham(Business_Objects.ChiTietThanhPhanThucPham ChiTietThanhPhanThucPham)
        {
            SQLUtility.TableName = "ChiTietThanhPhanThucPham";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(ChiTietThanhPhanThucPham)); }
            catch { return -1; }
        }

        public int DeleteChiTietThanhPhanThucPham(Business_Objects.ChiTietThanhPhanThucPham ChiTietThanhPhanThucPham)
        {
            SQLUtility.TableName = "ChiTietThanhPhanThucPham";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(ChiTietThanhPhanThucPham)); }
            catch { return -1; }
        }

        private DbParameter[] Take(ChiTietThanhPhanThucPham ChiTietThanhPhanThucPham)
        {
            DbParameter[] parameters = new DbParameter[4];
            parameters[0] = new SqlParameter("@Id", ChiTietThanhPhanThucPham.Id);
            parameters[1] = new SqlParameter("@ThucPhamId", ChiTietThanhPhanThucPham.ThucPhamId);
            parameters[2] = new SqlParameter("@ThanhPhanThucPhamId", ChiTietThanhPhanThucPham.ThanhPhanThucPhamId);
            parameters[3] = new SqlParameter("@LuongTrenMotDonVi", ChiTietThanhPhanThucPham.LuongTrenMotDonVi);
            return parameters;
        }
    }
}
